package com.leoman.plz_p.views.study;

import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.leoman.plz_p.R;
import com.leoman.plz_p.adapter.StudyAdapter;
import com.leoman.plz_p.interfaces.StudyView;
import com.leoman.plz_p.module.bean.ExamSubmitRequestBean;
import com.leoman.plz_p.module.bean.IdsBean;
import com.leoman.plz_p.module.bean.LoginSuccess;
import com.leoman.plz_p.module.bean.MapData;
import com.leoman.plz_p.module.bean.RecentExamBean;
import com.leoman.plz_p.module.bean.TestExamBean;
import com.leoman.plz_p.presenter.StudyPresenter;
import com.leoman.plz_p.views.homepage.SearchActivity;
import com.leoman.plz_p.weidget.NavBar;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import cn.com.corelib.base.BaseFragment;
import cn.com.corelib.subscribe.RxBusSubscriber;
import cn.com.corelib.utils.rxbus.RxBus;

/**
 * Created by guoh on 2018/8/26.
 */
public class StudyFragment extends BaseFragment<StudyView, StudyPresenter> implements StudyView {

    @Bind(R.id.nav)
    NavBar nav;
    @Bind(R.id.tv_recent_exam)
    TextView tvRecentExam;
    @Bind(R.id.ls_exam)
    ListView lsExam;
    @Bind(R.id.tv_search)
    TextView tvSearch;

    private String examId;
    private String examTitle;

    private StudyAdapter adapter;

    private TestExamBean currentExamBean;

    @Override
    protected int getLayoutId() {
        return R.layout.fr_study;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        nav.setBackIcon(R.mipmap.icon_yellow_people);
        nav.setTitle("模拟考试");
        nav.setBackClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        adapter = new StudyAdapter(getViewContext());
        lsExam.setAdapter(adapter);
        lsExam.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                currentExamBean=adapter.getItem(position);
                if (adapter.getItem(position).getStatus() == 1) {
                    //已经考试过
                    examId = adapter.getItem(position).getId() + "";
                    examTitle = adapter.getItem(position).getTitle();
                    getPresenter().getHasDoExamInfo(adapter.getItem(position).getId() + "");
                } else {
                    int examId = adapter.getItem(position).getId();
                    getPresenter().getExamIds2(examId+"");
                }
            }
        });
        tvRecentExam.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        initRxBus();
    }

    @Override
    public void onStart() {
        super.onStart();
        getPresenter().getExamList(null);
        getPresenter().getRecentExamInfo();
    }

    private void initRxBus(){
        RxBus.getDefault()
                .toObservable(LoginSuccess.class)
                .compose(this.<LoginSuccess>bindToLifecycle()).subscribe(new RxBusSubscriber<LoginSuccess>() {
            @Override
            public void receive(LoginSuccess data) {
                getPresenter().getExamList(null);
                getPresenter().getRecentExamInfo();
            }
        });
    }

    @Override
    protected StudyPresenter createPresenter() {
        return new StudyPresenter();
    }


    @Override
    public void obtainExamList(List<TestExamBean> rows) {
        adapter.replaceAll(rows);
    }

    @Override
    public void obtainRecentExamInfo(final MapData<RecentExamBean> data) {
        if (data.r == null) return;
        tvRecentExam.setText("最近的模拟考试:" + data.r.getTitle());
        tvRecentExam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPresenter().getExamIds(data.r.getId() + "");
            }
        });
    }

    @Override
    public void obtainHasDoExamInfo(IdsBean data) {
        List<ExamSubmitRequestBean> requestBeans = new Gson().fromJson(data.result, new TypeToken<List<ExamSubmitRequestBean>>() {
        }.getType());
        startActivity(AnswerActivity.getLaunchIntent(getViewContext(),examTitle,data.ids,examId,0,false,false,false,requestBeans));
    }

    @Override
    public void obtainExamInfo(MapData data) {
        if (data == null) return;
        //已经考试过
        examId = data.examin.getId() + "";
        examTitle = data.examin.getTitle();
        getPresenter().getHasDoExamInfo(examId + "");

    }

    @Override
    public void obtainIds(List ids) {
        startActivity(AnswerActivity.getLaunchIntent2(getViewContext(),currentExamBean.getTitle(),ids.toString().replace("[","").replace("]",""),currentExamBean.getId()+"",currentExamBean.getExaminTime(),false,true,false));
    }


    @OnClick(R.id.tv_search)
    public void onViewClicked() {
        startActivity(SearchActivity.getLaunchIntent(getViewContext()));
    }
}
